package com.lzx.adx.report.support.strategy.v2.channel;

import cn.hutool.core.util.URLUtil;
import com.alibaba.fastjson.JSONObject;
import com.lzx.adx.report.annnotions.ProductType;
import com.lzx.adx.report.constant.ProductChannelConstant;
import com.lzx.adx.report.dto.AccountDTO;
import com.lzx.adx.report.dto.ReportDTO;
import com.lzx.adx.report.util.URLEncoderDecoderTool;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

@Component
@Slf4j
@ProductType(ProductChannelConstant.LI_TIAN)
public class LiTianReportStrategy extends AbstractReport{
    @Override
    public String getReportFinalUrl(ReportDTO reportDTO, AccountDTO accountDTO) {

        String dspExtend = accountDTO.getDspExtend();
        JSONObject jsonObject = JSONObject.parseObject(dspExtend);
//        String access_id = jsonObject.getString("access_id");
//        String secret = jsonObject.getString("secret");
        String access_id = "tp7966e009fd87a95c7be5311238965c1e";
        String secret = "22c1a2e1c448241d2700dd40e6adb39e";
        long ts = System.currentTimeMillis() / 1000;
        Map<String, Object> pramsSigMap = new HashMap<>();
        pramsSigMap.put("access_id", access_id);
        pramsSigMap.put("ts", ts);
        String signature = URLUtil.buildQuery(pramsSigMap, StandardCharsets.UTF_8) + secret;
        Map<String, Object> prams = new HashMap<>();
        prams.put("os", "0");
        prams.put("ip", StringUtils.isNotBlank(reportDTO.getIp()) ? reportDTO.getIp() : "");
        prams.put("ua", StringUtils.isNotBlank(reportDTO.getUser_agent()) ? URLUtil.encode(reportDTO.getUser_agent()) : "");
        prams.put("ts", ts);
        prams.put("signature", signature);
        prams.put("access_id", access_id);
        prams.put("request_id", reportDTO.getBizRequestId());
        prams.put("imei", StringUtils.isNotBlank(reportDTO.getImei()) ? reportDTO.getImei() : "");
        prams.put("imei_md5", StringUtils.isNotBlank(reportDTO.getImeiSum()) ? reportDTO.getImeiSum() : "");
        prams.put("oaid", StringUtils.isNotBlank(reportDTO.getOaid()) ? reportDTO.getOaid() : "");
        prams.put("oaid_md5", StringUtils.isNotBlank(reportDTO.getOaidSum()) ? reportDTO.getOaidSum() : "");
        String callbackUrl = String.format(accountDTO.getDspCallbackUrl(), reportDTO.getBizRequestId());
        String urlParams = URLUtil.buildQuery(prams, StandardCharsets.UTF_8) + "&callback_url=" + URLEncoderDecoderTool.encodeURL(callbackUrl);
        return accountDTO.getDspReportUrl() + "&" + urlParams;
    }
}
